<template name="page_subjcet">
  <view id="page_subject">
    <!-- 导航栏 -->
    <action-bar :options="navOptions" />
    <view class="contain">
      <view class="row">
        <view class="col">
          <view>旧密码</view>
          <input type="password"
                 v-model="form.oldPassword"
                 placeholder="请输入旧密码" />
        </view>
        <view class="col">
          <view>新密码</view>
          <input :type="eyeState1 ? 'password' : 'text'"
                 maxlength="20"
                 v-model="form.newPassword"
                 placeholder="请输入新密码(8-20位)" />
          <img @click.native="eyeState1 = !eyeState1"
               :src="eyeState1 ? eyeHidden : eyeShow"
               alt="">
        </view>
        <span v-if="text">{{text}}</span>
        <view class="col">
          <view>确认密码</view>
          <input :type="eyeState2 ? 'password' : 'text'"
                 v-model="form.Password"
                 placeholder="请再次输入新密码" />
          <img @click.native="eyeState2 = !eyeState2"
               :src="eyeState2 ? eyeHidden : eyeShow"
               alt="">
        </view>
        <span v-if="text1">{{text1}}</span>
      </view>
      <view class="btn">
        <view @click="changeBtn">提交</view>
      </view>
    </view>
    <!-- 按钮 end -->
    <toast></toast>
  </view>
</template>
<script>

export default {
  data () {
    return {
      navOptions: { // 导航栏配置参数
        center: {
          title: '重置密码'
        },
        statusBar: 'dark'
      },
      form: {
        oldPassword: '',
        newPassword: '',
        Password: ''
      },
      text: null, // 新密码的校验信息
      text1: null, // 确认新密码的校验信息
      eyeState1: true,
      eyeState2: true,
      eyeShow: require('./show.png'),
      eyeHidden: require('./hidden.png')
    }
  },
  watch: {
    'form.newPassword': {
      handler (newVal) {
        let patrn = /^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\W_!@$*~()-+=]+$)(?![a-z0-9]+$)(?![a-z\W_!@$*~()-+=]+$)(?![0-9\W_!@$*~()-+=]+$)[a-zA-Z0-9\W_!@$*~()-+=]/;
        if (newVal.length < 8) {
          this.text = '密码长度不能低于8位有效字符！'
          return
        }
        if (newVal.indexOf('&') != '-1' || newVal.indexOf('#') != '-1' || newVal.indexOf('%') != '-1' || newVal.indexOf('^') != '-1') {
          this.text = '不能包含#、%、^、&等特殊字符'
          return
        }
        if (!patrn.test(newVal)) {
          this.text = '必须包含数字、英文字母、特殊符号等有效字符！'
          return
        }
        this.text = null
        if (this.form.Password != '' && newVal != this.form.Password) {
          this.text1 = '两次密码不一致！'
          return
        }
      },
      deep: true,
      immediate: false
    },
    'form.Password': {
      handler (newVal) {
        if (newVal != this.form.newPassword) {
          this.text1 = '两次密码不一致！'
        } else {
          this.text1 = null
        }
      },
      deep: true,
      immediate: false
    }
  },
  methods: {
    // 确认密码校验
    confirmPassword () {
      return new Promise((resovle, reject) => {
        const password = this.form.newPassword

        if (password !== this.form.Password) {
          reject('两次输入的密码不一致')

          this.$showToast({
            show: true,
            text: '两次输入的密码不一致'
          });
        } else {
          resovle(true)
        }
      })
    },

    // 校验密码长度
    passwordLength () {
      return new Promise((resovle, reject) => {
        const password = this.form.newPassword

        if (password.length < 6) {
          reject('密码最少六位')

          this.$showToast({
            show: true,
            text: '密码最少六位'
          });
        } else {
          resovle(true)
        }
      })
    },
    // 修改密码
    changeBtn () {
      if (this.form.oldPassword == '') {
        this.$showToast({
          show: true,
          text: '请输入旧密码'
        })
        return
      }
      if (this.form.newPassword == '') {
        this.$showToast({
          show: true,
          text: '请输入新密码'
        })
        return
      }
      if (this.form.Password == '') {
        this.$showToast({
          show: true,
          text: '请再次输入新密码'
        })
        return
      }
      if (this.form.Password != this.form.newPassword) {
        this.$showToast({
          show: true,
          text: '两次密码不一致'
        })
        return
      }
      if (this.text == null && this.text1 == null) {
        this.$store.dispatch('changePassword', this.form).then(res => {
          if (res && res.code === 200) {
            this.$showToast({
              show: true,
              text: '修改成功'
            })
            this.$store.commit('set_loginIp', '已修改密码')
            setTimeout(() => {
              uni.reLaunch({
                url: '/pages/layout/index'
              });
            }, 1000)
          } else {
            this.$showToast({
              show: true,
              text: res.msg
            })
          }
        })
      } else {
        this.$showToast({
          show: true,
          text: '请完善信息'
        })
        return
      }
    }
  }
}
</script>
<style lang="scss" scoped>
#page_subject {
  position: relative;
  overflow: hidden;
  height: 100vh;
  background: #ffffff;
  .contain {
    height: calc(100% - 82rpx);
    .row {
      margin-top: 25rpx;
      margin-left: 26rpx;
      .col {
        display: flex;
        font-size: 28rpx;
        font-family: Source Han Sans CN;
        font-weight: 400;
        color: #333333;
        padding: 26rpx 0;
        border-bottom: 1px solid #dddddd;
        align-items: center;
        position: relative;
        & > view:first-child {
          // flex: 1;
          width: 200rpx;
        }
        input {
          // flex: 1;
          width: 400rpx;
        }
        img {
          position: absolute;
          right: 40rpx;
          width: 60rpx;
          height: 30px;
          z-index: 9;
        }
      }
      span {
        width: calc(100% - 26rpx);
        text-align: right;
        display: block;
        color: red;
      }
    }
  }
}
.btn {
  position: fixed;
  width: 100%;
  height: 200rpx;
  background: #ffffff;
  bottom: 0;
  left: 0;
  & > view {
    height: 80rpx;
    border-radius: 10rpx;
    background: #5390ff;
    margin: 0 24rpx;
    text-align: center;
    line-height: 80rpx;
    font-size: 32rpx;
    color: #ffffff;
  }
}
</style>